set more off
set matsize 5000
pwd

use  pik  dob_* us_born pob_fips male using  ${raw_data}/irs1040_universe_kid_fullyr.dta, clear
sort pik
by pik: gen n=_n
keep if n==1
drop n

gen year=2019
tempfile ty2019
save `ty2019'


use ${raw_data}/irs1040_universe_kid_fullyr.dta, clear
append using ${raw_data}/irs1040_ty17_18_kid_fullyr.dta
append using `ty2019'
*append using `ty2020'
keep pik year dob_* us_born pob_fips male wage have_1040 fil_stat
keep if ( inrange(dob_year,1979,1982) |  inrange(dob_year,1984,1987) |  inrange(dob_year,1989,1992) )

destring pik, replace

sort pik year
xtset pik year

foreach x in dob_month dob_day dob_year {
	replace `x' = l.`x' if `x'==. & l.`x'~=.
}

rename year taxyr_out
********** Restrict to Kid's Tax Years at age 19+ **********
gen dob = mdy(dob_month, dob_day, dob_year)
format dob %tdnn/dd/CCYY

gen cutyr=.
forvalues yr=1970/2017 {
	replace cutyr = `yr' if inrange(dob,mdy(7,1,`yr'-1),mdy(6,30,`yr'))
}

gen agecyr = taxyr_out-cutyr
keep if inrange(agecyr,23,34)

drop cutyr

merge m:1 pik using ${clean_data}/parents_fullyr, keep(match master) gen(_m_parents) ///
	keepusing(taxyr_cut cutyr belowcut z /*pik_key_parent*/ mom_age mom_exactage *parent_fil_stat parent_maxage birth_order* /*sib_count parent_sched_se*/ i_obstaxyr *parent_have_1040 i_*eitc* *agi* dob_dow pob_fips male us_born)

sum if _m_parents==1
list in 1/10 if _m_parents==1
keep if _m_parents==3

do ${main}/dos/tax/build/build_support/kid_parentvar_cleanup_slim.do

gen i_k1 = (birth_order==1)

do ${main}/dos/tax/build/build_support/kid_outcomes_slim.do

merge m:1 pik using ${clean_data}/dec2010_clean, keep(match master) gen(_m_dec2010)
drop _m_dec2010
gen tyrc_Lmarried0 = (tyrc_Lfil_stat==2 |tyrc_Lfil_stat==3)
gen dob_year_mom = dob_year - mom_age if !missing(mom_age)
drop mom_age
gen mom_age = taxyr_cut - dob_year_mom if !missing(dob_year_mom)

capture drop year
gen year = taxyr_cut
gen hhsize = birth_order + 1
replace hhsize = birth_order + 2 if tyrc_Lmarried0==1
replace hhsize = 7 if hhsize>=7 & hhsize~=.
merge m:1 year hhsize using ${supp_data}/povline, keep(match master) gen(_m_pov) keepusing(povline)
rename povline povline_unadj
gen povline = povline_unadj*(cpi_jan/cpi_2015)
gen tyrc_prpov0 = tyrc_pragiz<=povline if povline!=.

capture drop cpi_jan cpi_2015 f f2

gen tyrc_teenmom = mom_age<=19 if mom_age!=.
gen tyrc_teenmom_exact = mom_exactage<20 if mom_exactage!=.

*white_non_hisp
*tyrc_pragiz
*tyrc_L1040

gen t = dob_month + (dob_year-1980)*12
gen tsqr = t^2
gen tcub = t^3

save ${clean_data}/taxtax_fullyr_clean, replace
